package com.example.springboote.controller;

import com.example.springboote.domian.User;
import com.example.springboote.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
public class AdminController {


    @Autowired
    private UserService userService;
    /**
     * 跳入登录页面
     * @return
     */
    @GetMapping("/")
    public ModelAndView zhuce(){
        ModelAndView mav = new ModelAndView("/login");
        return mav;
    }

    /**
     * 登录
     * @return
     */
    @GetMapping("index")
    public ModelAndView index(){
        ModelAndView mav = new ModelAndView("/index");
        mav.addObject("a","王聪");
        return mav;
    }


    /**
     * 展示列表
     * @return
     */
    @GetMapping("list")
    public ModelAndView list(){
        ModelAndView modelAndView = new ModelAndView("/list");
        List<User> byAll = userService.findByAll();
        modelAndView.addObject("users",byAll);
        return modelAndView;
    }


    /**
     * 登录判断
     * @param username
     * @param pasword
     * @return
     */
    @PostMapping("login")
    public @ResponseBody String login(@RequestParam(name = "username") String username,@RequestParam(name = "password") String pasword){
        User byUsername = userService.findByUsername(username);
        if (byUsername != null){
            if (byUsername.getPassword().equals(pasword)){
                return "1";
            }else {
                return "2";
            }
        }else {
            return "3";
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("delete")
    @ResponseBody
    public String delete(@RequestParam(name = "id") String id){
        String delete = userService.delete(id);
        return delete;
    }


    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public User delete(User user){
        User user1 =  userService.add(user);
        return user1;
    }

    /**
     * 跳转添加用户
     * @param user
     * @return
     */
    @GetMapping("register")
    public ModelAndView add(User user){
        ModelAndView modelAndView = new ModelAndView("/register");
        return modelAndView;
    }

}
